<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// +----------------------------------------------------------------------+
// | Treacle PHP Framework Version 0.10                                   |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007 Treacle.cn All rights reserved.					  |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the 'License');      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an 'AS IS' BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: BoPo <ibopo@126.com>										  |
// +----------------------------------------------------------------------+
/**
 * Treacle file_driver class file
 *
 * @author BoPo <ibopo@126.com>
 * @link http://www.treacle.cn/
 * @copyright Copyright &copy; 2007 Treacle
 * @license http://www.treacle.cn/license/
 * @version $Id$
 * @package caching
 */
/**
 * 文件缓存驱动
 *
 *
 * @package caching
 * @author BoPo <ibopo@126.com>
 * @since 0.10
 * @version	$Id $
 */
class file_driver extends Cache_driver
{
	/**
    * 构造函数,初始化一些参数
    * @param config 初始化配置
    */
	public function __construct($config=null)
	{
		parent::__construct($config);
		if(!is_writeable($this->_cachedir))
		{
			throw new TSystemException('cache_dir_no_write');
		}
		$this->_prefix = 'file_';
	}
	/**
	 * 获取一个标识的值
	 * @param string $key 为一个唯一的标示
	 * @return string 跟这个标示相对应的值
	 */
	protected function getValue($key)
	{
		if (file_exists($this->_cachedir.$key))
		{
			$data = unserialize(file_get_contents($this->_cachedir.$key));
			if ($data[1]<time() && $data[1]!=0)
			{
				return null;
			}
			return $data[0];
		}
		return null;
	}
	/**
	 * 设置一个标识的值
	 * @param string $key 为一个已经存在的标示
	 * @param string 值
	 * @param integer 给标示存活期限
	 * @return boolean 
	 */
	protected function setValue($key,$value,$expire)
	{
		$this->deleteValue($key);
		return $this->addValue($key,$value,$expire);
	}
	/**
	 * 追加一个标识的值,并设置期限
	 * @param string $key 一个没有创建的标示
	 * @param string 需要缓存的值
	 * @param integer 给标示存活期限
	 * @return boolean
	 */
	protected function addValue($key,$value,$expire)
	{
		$value = array($value,$expire);
		return file_put_contents($this->_cachedir.$key,serialize($value));
	}
	/**
	 * 删除一个标识的值
	 * @param string 要删除的标示
	 * @return boolean
	 */
	protected function deleteValue($key)
	{
		if(file_exists($this->_cachedir.$key))
		{
			return unlink($this->_cachedir.$key);
		}
		return false;
	}
	/**
	 * 清空缓存中的所有内容
	 */
	public function flush()
	{
		if ($handle = opendir($this->_cachedir))
		{
			while (false !== ($file = readdir($handle)))
			{
				if (is_file($this->_cachedir.$file))
				{
					unlink($this->_cachedir.$file);
				}
			}
			closedir($handle);
		}
	}
}
/*
+---------------------------------------------------------------------------
|   Last Modified By BoPo
|   Date: Wed Nov 07 23:51:56 CST 2007
+--------------------------------------------------------------------------
*/
?>